app.controller('receiveOrderCtrl', ['$scope','appFactory','BaseService','$stateParams','$ionicLoading','$cordovaBarcodeScanner',
    function($scope,appFactory,BaseService,$stateParams,$ionicLoading,$cordovaBarcodeScanner) {
     $scope.getOrdersArray = function(){
         var uid = appFactory.getUserInfo().uid;
         var areaId = appFactory.getUserInfo().user.area;
         async.waterfall([
             function(callback){
                 var Order = AV.Object.extend("Order");
                 var orderQuery = new AV.Query(Order);
                 var ORDER_STATUS = [2500,3000,4000,4500,5000,6000];
                 orderQuery.containedIn('status',ORDER_STATUS);
                 orderQuery.equalTo("syncStatus",0);
                 orderQuery.containedIn("areaId",areaId);
                 orderQuery.find().then(function(res){
                     $scope.$apply(function(){
                         $scope.ordersArray = res;
                         callback(null);
                     });
                 }).catch(function(err){
                     console.log(err);
                     callback(err);
                 }).finally(function(){
                     $scope.$broadcast('scroll.refreshComplete');
                 });
             },
         ],function(err,result){
            if(err){
                console.log(err);
            }else{
                console.log("OK");
            }
         });
     }
     $scope.getOrdersArray();


    $scope.barscanReceive = function(){
        $cordovaBarcodeScanner
            .scan()
            .then(function(barcodeData) {

                //alert(JSON.stringify(barcodeData));
                var cancelled = barcodeData.cancelled;
                var result = JSON.parse(barcodeData.text);
                //alert('cancelled:'+cancelled);
                //alert('result:'+result);
                if(!cancelled){
                    var oid = result.OID;
                    var storage = window.localStorage;
                    var uid = storage.getItem("uid");
                    if (oid){
                        var Order = AV.Object.extend("Order");
                        var orderQuery = new AV.Query(Order);
                        orderQuery.equalTo('orderid',oid);
                        orderQuery.first().then(function(res){
                            BaseService.barscanHandle(uid,oid).then(function(res){
                                //alert(res.msg);
                                var r=confirm(res.msg+"\r\n是否继续扫码!");
                                if (r==true) {
                                    $scope.barscanReceive();
                                }
                            }).catch(function(err){
                                //alert(err.msg);
                                var r=confirm(res.msg+"\r\n是否继续扫码!");
                                if (r==true) {
                                    $scope.barscanReceive();
                                }
                            });
                        }).catch(function(err){
                            var r=confirm("没有扫描到正确的订单信息!\r\n是否继续扫码!");
                            if (r==true) {
                                $scope.barscanReceive();
                            }
                        });
                    }else{
                        var r=confirm("没有扫描到正确的订单信息!\r\n是否继续扫码!");
                        if (r==true) {
                            $scope.barscanReceive();
                        }
                    }
                }else{
                    alert('扫描异常或退出扫描');
                }
            }, function(error) {
                // An error occurred
            });
    }
}]);
